package Passageiros;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Vo.VooHasTrechoVoosVO;


public class VooHasTrechoVoosDAO extends DataSourceDAO  
{
	public VooHasTrechoVoosDAO( ) 
	{
		
	}

    public VooHasTrechoVoosVO consultarVooHasTrechoVoos( int codigo ) throws DAOException  
    {
    	VooHasTrechoVoosVO vooHasTrechoVoos = null;
        //String select = "SELECT * FROM VOOHASTRECHOVOOS WHERE CODIGO = ?";
    	
    	//inner join 
    	//String select = "SELECT t.descricaoTrecho, pt.precoCrianca, pt.precoAdulto, pt.preco"
        String select = "";
    	PreparedStatement pstmt	= null;
        ResultSet rs = null;
        
        try 
        {
        	connect( ) ;
            pstmt = connection.prepareStatement( select );
            pstmt.setInt( 1, codigo ) ;
            rs = pstmt.executeQuery( ) ;
            if ( rs.next() ) 
            {
                int codigoTrecho = rs.getInt("CODIGOTRECHO");
                int codigoPrecoTrecho = rs.getInt("CODIGOPRECOTRECHO");
                int codigoAeroporto = rs.getInt("CODIGOAEROPORTO");
                int codigoAeronave = rs.getInt("CODIGOAERONAVE");
                
                vooHasTrechoVoos = new VooHasTrechoVoosVO( );
                vooHasTrechoVoos.setCodigoVooHasTrechoVoo( codigo );
                vooHasTrechoVoos.setCodigoTrecho(codigoTrecho);
                vooHasTrechoVoos.setCodigoPrecoTrecho(codigoPrecoTrecho);
                vooHasTrechoVoos.setCodigoAeroporto(codigoAeroporto);
                vooHasTrechoVoos.setCodigoAeronave(codigoAeronave);
            }
        } 
        catch ( SQLException e ) 
        {
            e.printStackTrace();
            throw new DAOException("Impossivel consultar assentos!", e );
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                if ( rs != null ) 
                {
                    rs.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( ) ;
            }
        }
        return vooHasTrechoVoos ;
    }
    
	public void inserirVooHasTrechoVoos( VooHasTrechoVoosVO vooHasTrechoVoos ) throws DAOException  
    {
        String insert = "INSERT INTO VOOHASTRECHOVOOS ( CODIGO, CODIGOTRECHO, CODIGOPRECOTRECHO, CODIGOAEROPORTO, CODIGOAERONAVE) VALUES (?,?,?,?,?)";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement( insert );
            pstmt.setInt(1,vooHasTrechoVoos.getCodigoVooHasTrechoVoo());
            pstmt.setInt(2,vooHasTrechoVoos.getCodigoTrecho());
            pstmt.setInt(3,vooHasTrechoVoos.getCodigoPrecoTrecho());
            pstmt.setInt(4,vooHasTrechoVoos.getCodigoAeroporto());
            pstmt.setInt(5,vooHasTrechoVoos.getCodigoAeronave());
        
            pstmt.executeUpdate( ) ;
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel inserir voos!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( );
            }
        }
    }
    
    public void excluirVooHasTrechoVoos( VooHasTrechoVoosVO vooHasTrechoVoos )  throws DAOException  
    {
        String delete = "DELETE FROM VOOHASTRECHOVOOS WHERE CODIGO = ?";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement( delete );
            pstmt.setInt( 1,vooHasTrechoVoos.getCodigoVooHasTrechoVoo( ) ) ;
            pstmt.executeUpdate( ) ;
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel deletar vooHasTrechoVoos!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( ) ;
            }
        }
    }
    
    public void alterarVooHasTrechoVoos( VooHasTrechoVoosVO vooHasTrechoVoos ) throws DAOException  
    {
        String insert = "UPDATE VOOHASTRECHOVOOS SET CODIGOTRECHO = ?,CODIGOPRECOTRECHO = ?, CODIGOAEROPORTO = ?, CODIGOAERONAVE = ? WHERE CODIGO = ?";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement(insert);
                    
            pstmt.setInt(1,vooHasTrechoVoos.getCodigoTrecho());
            pstmt.setInt(2,vooHasTrechoVoos.getCodigoPrecoTrecho());
            pstmt.setInt(3,vooHasTrechoVoos.getCodigoAeroporto());
            pstmt.setInt(4,vooHasTrechoVoos.getCodigoAeronave());
            pstmt.setInt(5,vooHasTrechoVoos.getCodigoVooHasTrechoVoo());
            
            pstmt.executeUpdate ();
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel alterar vooHasTrechoVoos!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace();
            }
        }
    }
}
